#[get("/own_msg_list?<p>")]
fn own_msg_list(p: usize, cookies: Cookies, state: State<MySqlDriver>) -> Response<'static> {
    let mut res = Response::new();
    res.set_header(ContentType::HTML);

    let user_info: UserInfo = get_cookies(&cookies);
    if user_info.id == 0 {
        let html: String = format!("您未登录，请先<a href=\"{}login\">登录</a>", state.base_url.as_str());
        res.set_sized_body(Cursor::new(html));
        return res;
    }

    // mod_id: String, 
    // mod_menu_open: String, 
    // sub_mod_id: String, 
    // page_title: String, 
    // title: String
    let header: String = get_header(
        "msg-mod".to_string(), 
        "msg-mod-menu-open".to_string(), 
        "own-msg-list".to_string(),
        "用户后台 - 留言".to_string(), 
        "留言".to_string(), &cookies,
        &state);

    let pool_result: Result<Pool, mysql::Error> = Pool::new(state.driver.as_str());
    let pool: Pool;
    if let Ok(p) = pool_result {
        pool = p;
    } else {
        res.set_sized_body(Cursor::new("无法建立数据库连接".to_string()));
        return res;
    }
    let conn_result: Result<PooledConn, mysql::Error> = pool.get_conn();
    let mut conn: PooledConn;
    if let Ok(c) = conn_result {
        conn = c;
    } else {
        res.set_sized_body(Cursor::new("无法获取数据库连接".to_string()));
        return res;
    }
    let mut sql: String = format!("SELECT COUNT(`m`.`id`) AS `count` FROM `message` AS `m` WHERE `sender_id` = {}", user_info.id);
    let row2: Row = conn.query_first(sql.clone()).unwrap().unwrap();
    let count: usize = from_row(row2);
    let max_page: usize = ceil((count / 10) as f64, 0) as usize;
    let skip_number: usize = (p - 1) * 10;

    sql = format!("SELECT `m`.`id`, `m`.`title` FROM `message` AS `m` WHERE `sender_id` = {} LIMIT {}, 10", user_info.id, skip_number);
    let rows: Vec<Row> = conn.query(sql.clone()).unwrap();
    let mut tr: String = String::new();
    for row in rows {
        let (id, title): (usize, String) = from_row(row);
        tr.push_str(format!("<tr>\
                          <th scope=\"row\">{}</th>\
                          <td>{}</td>\
                          <td>\
                            <a class=\"btn btn-primary\" target=\"_blank\" href=\"view_msg?id={}&is_me=1\">查看</a>\
                          </td>\
                        </tr>", id, title.as_str(), id).as_str());
    }

    let mut is_prev_disabled: String = String::new();
    let mut is_next_disabled: String = String::new();
    if p <= 1 {
        is_prev_disabled.push_str(" disabled");
    }
    if p >= max_page {
        is_next_disabled.push_str(" disabled");
    }
    let mut nav: String = format!("<nav>\
                                    <ul class=\"pagination justify-content-center\">\
                                        <li class=\"page-item{}\">\
                                            <a class=\"page-link\" href=\"own_msg_list?p={}\" aria-label=\"Previous\">\
                                                <span aria-hidden=\"true\">&laquo;</span>\
                                            </a>\
                                        </li>", is_prev_disabled.as_str(), p - 1);
    for i in 1..=max_page {
        if p == i {
            nav.push_str(format!("<li class=\"page-item active\"><a class=\"page-link\" href=\"javascript:void(0);\">{}</a></li>", i).as_str());
        } else {
            nav.push_str(format!("<li class=\"page-item\"><a class=\"page-link\" href=\"own_msg_list?p={}\">{}</a></li>", i, i).as_str());
        }
    }
    nav.push_str(format!("<li class=\"page-item{}\">\
                            <a class=\"page-link\" href=\"own_msg_list?p={}\" aria-label=\"Next\">\
                                <span aria-hidden=\"true\">&raquo;</span>\
                            </a>\
                          </li>\
                      </ul>\
                    </nav>", is_next_disabled.as_str(), p + 1).as_str());
    
    let mut src_vec: Vec<&[u8]> = Vec::new();
    let mut replace_vec: Vec<&[u8]> = Vec::new();

    let footer_string: String = get_footer();
    src_vec.push("{header}".as_bytes());
    src_vec.push("{base_url}".as_bytes());
    src_vec.push("{tr}".as_bytes());
    src_vec.push("{nav}".as_bytes());
    src_vec.push("{footer}".as_bytes());
    replace_vec.push(header.as_bytes());
    replace_vec.push(state.base_url.as_bytes());
    replace_vec.push(tr.as_bytes());
    replace_vec.push(nav.as_bytes());
    replace_vec.push(footer_string.as_bytes());

    let mut page_str: HoString = HoFile::read_file_bytes("web/own_msg_list.html");
    page_str = page_str.replace_bytes_vec(src_vec, replace_vec);
    let html: String = page_str.to_str().to_string().to_owned();
    res.set_sized_body(Cursor::new(html));
    return res;
}

#[get("/receive_msg_list?<p>")]
fn receive_msg_list(p: usize, cookies: Cookies, state: State<MySqlDriver>) -> Response<'static> {
    let mut res = Response::new();
    res.set_header(ContentType::HTML);

    let user_info: UserInfo = get_cookies(&cookies);
    if user_info.id == 0 {
        let html: String = format!("您未登录，请先<a href=\"{}login\">登录</a>", state.base_url.as_str());
        res.set_sized_body(Cursor::new(html));
        return res;
    }

    // mod_id: String, 
    // mod_menu_open: String, 
    // sub_mod_id: String, 
    // page_title: String, 
    // title: String
    let header: String = get_header(
        "msg-mod".to_string(), 
        "msg-mod-menu-open".to_string(), 
        "receive-msg-list".to_string(),
        "用户后台 - 留言".to_string(), 
        "留言".to_string(), &cookies,
        &state);

    let pool_result: Result<Pool, mysql::Error> = Pool::new(state.driver.as_str());
    let pool: Pool;
    if let Ok(p) = pool_result {
        pool = p;
    } else {
        res.set_sized_body(Cursor::new("无法建立数据库连接".to_string()));
        return res;
    }
    let conn_result: Result<PooledConn, mysql::Error> = pool.get_conn();
    let mut conn: PooledConn;
    if let Ok(c) = conn_result {
        conn = c;
    } else {
        res.set_sized_body(Cursor::new("无法获取数据库连接".to_string()));
        return res;
    }
    let mut sql: String = format!("SELECT COUNT(`m`.`id`) AS `count` FROM `message` AS `m` INNER JOIN `copywrite_order` AS `copy_o` ON `copy_o`.`id` = `m`.`copywrite_order_id` INNER JOIN `tag_group` AS `tg` ON `tg`.`id` = `m`.`receive_group_id` WHERE `m`.`receive_user_id` = {} OR `tg`.`admin_id_examine` = {} OR `tg`.`admin_id_publish` = {}", user_info.id, user_info.id, user_info.id);
    let row2: Row = conn.query_first(sql.clone()).unwrap().unwrap();
    let count: usize = from_row(row2);
    let max_page: usize = ceil((count / 10) as f64, 0) as usize;
    let skip_number: usize = (p - 1) * 10;

    sql = format!("SELECT `m`.`id`, `u`.`account`, `m`.`title`, `m`.`is_read` FROM `message` AS `m` INNER JOIN `copywrite_order` AS `copy_o` ON `copy_o`.`id` = `m`.`copywrite_order_id` INNER JOIN `tag_group` AS `tg` ON `tg`.`id` = `m`.`receive_group_id` INNER JOIN `user` AS `u` ON `u`.`id` = `m`.`sender_id` WHERE `m`.`receive_user_id` = {} OR `tg`.`admin_id_examine` = {} OR `tg`.`admin_id_publish` = {} ORDER BY `m`.`is_read` ASC, `m`.`id` DESC LIMIT {}, 10", user_info.id, user_info.id, user_info.id, skip_number);
    let rows: Vec<Row> = conn.query(sql.clone()).unwrap();
    let mut tr: String = String::new();
    for row in rows {
        let (id, account, title, is_read): (usize, String, String, u8) = from_row(row);
        let read_status: &str;
        if is_read == 1 {
            read_status = "是";
        } else {
            read_status = "否";
        }
        tr.push_str(format!("<tr>\
                      <th scope=\"row\">{}</th>\
                      <td>{}</td>\
                      <td>{}</td>\
                      <td>{}</td>\
                      <td>\
                        <a class=\"btn btn-primary\" target=\"_blank\" href=\"view_msg?id={}\">查看</a>\
                      </td>\
                    </tr>", id, account.as_str(), title.as_str(), read_status, id).as_str());
    }

    let mut is_prev_disabled: String = String::new();
    let mut is_next_disabled: String = String::new();
    if p <= 1 {
        is_prev_disabled.push_str(" disabled");
    }
    if p >= max_page {
        is_next_disabled.push_str(" disabled");
    }
    let mut nav: String = format!("<nav>\
                                    <ul class=\"pagination justify-content-center\">\
                                        <li class=\"page-item{}\">\
                                            <a class=\"page-link\" href=\"receive_msg_list?p={}\" aria-label=\"Previous\">\
                                                <span aria-hidden=\"true\">&laquo;</span>\
                                            </a>\
                                        </li>", is_prev_disabled.as_str(), p - 1);
    for i in 1..=max_page {
        if p == i {
            nav.push_str(format!("<li class=\"page-item active\"><a class=\"page-link\" href=\"javascript:void(0);\">{}</a></li>", i).as_str());
        } else {
            nav.push_str(format!("<li class=\"page-item\"><a class=\"page-link\" href=\"receive_msg_list?p={}\">{}</a></li>", i, i).as_str());
        }
    }
    nav.push_str(format!("<li class=\"page-item{}\">\
                            <a class=\"page-link\" href=\"receive_msg_list?p={}\" aria-label=\"Next\">\
                                <span aria-hidden=\"true\">&raquo;</span>\
                            </a>\
                          </li>\
                      </ul>\
                    </nav>", is_next_disabled.as_str(), p + 1).as_str());
    
    let mut src_vec: Vec<&[u8]> = Vec::new();
    let mut replace_vec: Vec<&[u8]> = Vec::new();

    let footer_string: String = get_footer();
    src_vec.push("{header}".as_bytes());
    src_vec.push("{base_url}".as_bytes());
    src_vec.push("{tr}".as_bytes());
    src_vec.push("{nav}".as_bytes());
    src_vec.push("{footer}".as_bytes());
    replace_vec.push(header.as_bytes());
    replace_vec.push(state.base_url.as_bytes());
    replace_vec.push(tr.as_bytes());
    replace_vec.push(nav.as_bytes());
    replace_vec.push(footer_string.as_bytes());

    let mut page_str: HoString = HoFile::read_file_bytes("web/receive_msg_list.html");
    page_str = page_str.replace_bytes_vec(src_vec, replace_vec);
    let html: String = page_str.to_str().to_string().to_owned();
    res.set_sized_body(Cursor::new(html));
    return res;
}
